/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import Tablas.Libros;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.HibernateException;
/**
 *
 * @author carlosenriquez
 */

    public class UserDAO_Libros
    {
       public static List<Libros> getCustomer()
      {
          
             Session session = null;
             Configuration configuration=new Configuration();
             SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
             session=sessionFactory.openSession();
             Transaction transaction=session.beginTransaction();
         try
         {
            configuration.configure();
            session.beginTransaction();
            String HQL_QUERY= "from Libros libros";
            org.hibernate.Query query= session.createQuery(HQL_QUERY);
            
            System.out.println("Reading values...");
            
            List registro=query.list();
            List<Libros> act=new ArrayList<Libros>();
            
            boolean found=false;
            for(Iterator it= query.iterate(); it.hasNext();)
            {
                
                Libros lib= (Libros) it.next();
                
                
                act.add(lib);
                found=true;
            }
            session.getTransaction().commit();
            System.out.println("Done!");
            
            
            
           if(found)
           {
             return act;
           }
           
            
         }catch(Exception e)
         {
           System.out.println(e.getMessage());
         }
         finally
         {
           session.flush();
           session.close();
           }        
          return null;
         }
       public void Guardar (Libros libros){
       Session session = null;
             Configuration configuration=new Configuration();
             SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
             session=sessionFactory.openSession();
             Transaction transaction=session.beginTransaction();
             try{
                 session.beginTransaction();
                 session.save(libros);
                 session.getTransaction().commit();
             }
             catch(HibernateException e){
                
             }
       }
       public void Borrar (Libros libros){
       Session session = null;
             Configuration configuration=new Configuration();
             SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
             session=sessionFactory.openSession();
             Transaction transaction=session.beginTransaction();
             try{
                 session.beginTransaction();
                 session.delete(libros);
                 session.getTransaction().commit();
             }
             catch(HibernateException e){
             }
       }
   }
